﻿using FrameWorkCore;
using FrameWorkCore.EFCore;
using FrameWorkCore.DynamicWebApi;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.AspNetCore.Mvc.Controllers;
using Entity.Enum;
using FrameWorkCore.CustomFilter;
using NLog.Web;
using Microsoft.AspNetCore.Mvc;
using FrameWorkCore.ResponseCompression;
using FrameWorkCore.HangFire;
using FrameWorkCore.IdentityServer4;

namespace WebApi6
{



    public class Services
    {

        /// <summary>
        /// 服务注册中心
        /// </summary>
        /// <param name="args"></param>
        /// <returns></returns>
        public static WebApplicationBuilder ServicesRun(string[] args)
        {
            var builder = WebApplication.CreateBuilder(args);


            // NLog: Setup NLog for Dependency injection
            builder.Logging.ClearProviders();
            builder.Logging.SetMinimumLevel(Microsoft.Extensions.Logging.LogLevel.Trace);
            builder.Host.UseNLog();

            var Services = builder.Services;

            // Add services to the container.

            Services.AddCors(options => { options.AddPolicy("AllowAnonymous", op => { op.AllowAnyOrigin().AllowAnyHeader().AllowAnyMethod(); }); });

            Services.Configure<ApiBehaviorOptions>( opt => opt.SuppressModelStateInvalidFilter = true);

            Services.AddRouting(options => options.LowercaseUrls = true);

            Services.TryAddSingleton<IHttpContextAccessor, HttpContextAccessor>();

            //Services.ConfigureIdentityServer4();

            Services.AddHttpContextAccessor();

            Services.AddHttpClient();

            Services.ConfigureJwt(builder.Configuration);

            Services.ConfigureHangFire(builder.Configuration);

            Services.AddMvc(option => 
            { 
                option.Filters.Add<CustomActionFilter>(); 
                option.Filters.Add<CustomExceptionFilter>();
                option.SuppressImplicitRequiredAttributeForNonNullableReferenceTypes = true;

            })
            .AddControllersAsServices().ConfigureNewtonsoftJson(); 

            Services.ConfigureEFCore(builder.Configuration, DefaultDatabaseType.SqlServer);

            // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
            Services.AddEndpointsApiExplorer();

            Services.ConfigureDynamicWebApi();

            Services.ConfigureSwagger();

            Services.ConfigureResponseCompression();

            //Services.AddResponseCaching();

            //Services.BuildServiceProvider(true);

            return builder;
        }    
    }
 }
